package cn.jbit.ams.order.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import cn.jbit.ams.mainperson.biz.MainpersonService;
import cn.jbit.ams.order.biz.OrderService;
import cn.jbit.ams.order.dao.OrderMapper;
import cn.jbit.ams.user.pojo.Mainperson;
import cn.jbit.ams.user.pojo.Order;
import cn.jbit.ams.user.pojo.Productinfo;
import net.sf.json.JSONArray;

@Controller
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private MainpersonService mainService;
	
	@ResponseBody
	@RequestMapping("getOrder")
	public String getOrder(Integer mId) {
		List<Order> orderList= orderService.getOrder(mId);
		String orderJson=JSON.toJSONString(orderList);
		return orderJson;
	}
	
	@ResponseBody
	@RequestMapping("/ajax_02")
	public JSONArray select(String id) {
		System.out.println(id+"wqqw");
		JSONArray date = JSONArray.fromObject(orderService.select(id));   
		System.out.println(date);
		return date;	
	}
	@RequestMapping("/ajax_03")
	public ModelAndView insert(Mainperson m,Order order,Model model) {
		System.out.println(m.getmName()+"dasdas");
		System.out.println(order.getOrderId()+"qqqq");
		System.out.println(m.getmId()+"www");	
		orderService.update(m.getmId(),order.getOrderId());
		return new ModelAndView("redirect:/dologin?userName="+123+"&password="+123456); 
	} 

	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping("/ajax_022") public JSONArray select2(String id) {
	 * System.out.println(id+"1314"); JSONArray date =
	 * JSONArray.fromObject(mainService.ajax_01(id)); System.out.println(date);
	 * return date; }
	 */
	@RequestMapping("paigong")
	public String paigong(String mId,String oId) {
		System.out.println(mId+","+oId);
		orderService.paigong(oId, mId); 
		return "index";	
	}
	
	@ResponseBody
	@RequestMapping("optionOrder")
	public ModelAndView getoptionOrder(int state,Integer pageNum,Model model) {
		if (pageNum==null) {
			pageNum=1;
		}
		Page<Order> orderPage=orderService.getoptionOrder(state, pageNum);
		model.addAttribute("orderPage", orderPage);
		return new ModelAndView("redirect:/dologin?userName="+123+"&password="+123456+"&state="+state);
	}
	
	
	@RequestMapping("getMainperson")
	public String getMaineprson(Integer productInfoId,Model model) {
		Productinfo proinfo=orderService.getOneInfo(productInfoId);
		List<Mainperson> mainList=mainService.mainpersonList(proinfo.getProduct().getProductId());
		String mainJson=JSON.toJSONString(mainList);
		return mainJson;
	}
	
	

	
}
